<?php

/**
 * This is the model class for table "order".
 *
 * The followings are the available columns in table 'order':
 * @property integer $identification
 * @property integer $account
 * @property integer $pay_method
 * @property string $name
 * @property string $phone
 * @property string $address
 * @property integer $total_amount
 * @property double $total_price
 * @property string $delivery_date
 * @property string $information
 * @property string $note
 * @property string $insert
 * @property string $update
 * @property string $delete
 *
 * The followings are the available model relations:
 * @property Account $account0
 * @property OrderDetail[] $orderDetails
 */
class Order extends CActiveRecord
{
    //Constance
    const PAYMETHOD_CASH            = 1;
    const PAYMETHOD_PAYMENT_GATEWAY = 2;
    const PAYMETHOD_BANK_TRANSFER   = 3;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Order the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'order';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, phone, address, total_amount, total_price', 'required'),
            array('account, pay_method, total_amount', 'numerical', 'integerOnly'=>true),
            array('total_price', 'numerical'),
            array('name, address', 'length', 'max'=>255),
            array('phone', 'length', 'max'=>100),
            array('delivery_date, information, note, insert, update, delete', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('identification, account, pay_method, name, phone, address, total_amount, total_price, delivery_date, information, note, insert, update, delete', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'account0' => array(self::BELONGS_TO, 'Account', 'account'),
            'orderDetails' => array(self::HAS_MANY, 'OrderDetail', 'order'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'identification' => 'Identification',
            'account' => 'Account',
            'pay_method' => 'Pay Method',
            'name' => 'Name',
            'phone' => 'Phone',
            'address' => 'Address',
            'total_amount' => 'Total Amount',
            'total_price' => 'Total Price',
            'delivery_date' => 'Delivery Date',
            'information' => 'Information',
            'note' => 'Note',
            'insert' => 'Insert',
            'update' => 'Update',
            'delete' => 'Delete',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('identification',$this->identification);
        $criteria->compare('account',$this->account);
        $criteria->compare('pay_method',$this->pay_method);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('phone',$this->phone,true);
        $criteria->compare('address',$this->address,true);
        $criteria->compare('total_amount',$this->total_amount);
        $criteria->compare('total_price',$this->total_price);
        $criteria->compare('delivery_date',$this->delivery_date,true);
        $criteria->compare('information',$this->information,true);
        $criteria->compare('note',$this->note,true);
        $criteria->compare('insert',$this->insert,true);
        $criteria->compare('update',$this->update,true);
        $criteria->compare('delete',$this->delete,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }


    /** addDataToDb =============================
     * @author   TuanNguyen
     * @desc     save order, order_detail entity to database
     * @param    string $p_insertStaement: string for values in insert statement
     * @return   boolean
     *           ErrorEntity: if there are an error in database
     * ============================================== */
    public static function addDataToDb(Order $o_order, $s_orderDetailStatement)
    {
        $command = Yii::app()->db->createCommand("CALL order_03_insert
            (
                :p_account,
                :p_pay_method,
                :p_name,
                :p_phone,
                :p_address,
                :p_total_amount,
                :p_total_price,
                :p_delivery_date,
                :p_information,
                :p_pistatement
            )");

        $command->bindValue("p_account", $o_order->account,PDO::PARAM_INT);
        $command->bindValue("p_pay_method", $o_order->pay_method);
        $command->bindValue("p_name", $o_order->name);
        $command->bindValue("p_phone", $o_order->phone);
        $command->bindValue("p_address", CHtml::encode($o_order->address));
        $command->bindValue("p_total_amount", $o_order->total_amount, PDO::PARAM_INT);
        $command->bindValue("p_total_price",$o_order->total_price);
        $command->bindValue("p_delivery_date",$o_order->delivery_date);
        $command->bindValue("p_information",CHtml::encode($o_order->information));
        $command->bindValue("p_pistatement",$s_orderDetailStatement);

        try
        {
            $i_result = $command->execute();

            if($i_result >=1)
                return true;

            return false;
        }
        catch (CDbException $ex)
        {
            $o_error            = new ErrorEntity();
            $o_error->errCode   = "ERR_2";
            $o_error->param     = array("SQLException" => $ex->getMessage());
            return $o_error;
        }
    }//End addDataToDb
}//End class